﻿Imports System.IO
Imports System.Text.RegularExpressions

Public Module ConfigPropertyKeys

	Public lastCalTime As DateTime

	Private db As New CongregationDataContainerLocal

	Private _ReportLastSubmitted As DateTime? = Nothing
	Public ReadOnly Property ReportLastSubmitted As DateTime
		Get

			If _ReportLastSubmitted Is Nothing OrElse (Now.AddSeconds(-1)) > lastCalTime Then
				Dim dbDate = db.MonthlyReportDatas.Select(Function(x) x.Month)
				_ReportLastSubmitted = Date.SpecifyKind(If(dbDate.Count = 0, DateTime.MinValue, dbDate.Max), DateTimeKind.Utc)

				lastCalTime = Now
			End If

			Return _ReportLastSubmitted.Value

		End Get
	End Property

	Private _PreviousReportLastSubmitted As Date? = Nothing
	Public ReadOnly Property PreviousReportLastSubmitted As Date
		Get
			If _PreviousReportLastSubmitted Is Nothing OrElse (Now.AddSeconds(-1)) > lastCalTime Then
				'we are getting all the dates and then taking the second-most recent one
				Dim dbDate = db.MonthlyReportDatas.Select(Function(x) x.Month)
				_PreviousReportLastSubmitted = Date.SpecifyKind(If(dbDate.Count < 2, DateTime.MinValue, dbDate.OrderByDescending(Function(x) x).Skip(1).First), DateTimeKind.Utc)

				lastCalTime = Now
			End If

			Return _PreviousReportLastSubmitted.Value

		End Get
	End Property

	Public Property AutoBackupLocation As DirectoryInfo
		Get
			If GetKeyValue("AutoBackupLocation") IsNot Nothing Then
				Dim fileLocation As New DirectoryInfo(GetKeyValue("AutoBackupLocation"))
				If fileLocation.Exists Then
					AutoBackupLocation = fileLocation
				Else
					AutoBackupLocation = Nothing
				End If
			Else
				AutoBackupLocation = Nothing
			End If

			If AutoBackupLocation Is Nothing Then
				AutoBackupLocation = New DirectoryInfo(My.Application.Info.DirectoryPath)
			End If

			Return AutoBackupLocation
		End Get
		Set(value As DirectoryInfo)
			UpsertKeyValue("AutoBackupLocation", value.FullName)
		End Set
	End Property

	Public Property S21VerticalOffset As Integer
		Get
			If GetKeyValue("S21VerticalOffset") IsNot Nothing Then
				Return GetKeyValue("S21VerticalOffset")
			Else
				Return 0
			End If
		End Get
		Set(value As Integer)
			UpsertKeyValue("S21VerticalOffset", value)
		End Set
	End Property

	Public Property S21Language As String
		Get
			If GetKeyValue("S21Language") IsNot Nothing Then
				Return GetKeyValue("S21Language")
			Else
				Return "E"
			End If
		End Get
		Set(value As String)
			UpsertKeyValue("S21Language", value)
		End Set
	End Property

	Public Property CongergationName As String
		Get
			Return GetKeyValue("CongregationName")
		End Get
		Set(value As String)
			UpsertKeyValue("CongregationName", value)
		End Set
	End Property

	Public ReadOnly Property AutoBackupFile As FileInfo
		Get
			Dim myAutoBackupLocation As DirectoryInfo = ConfigPropertyKeys.AutoBackupLocation

			Dim fileName As String = Regex.Replace(ConfigPropertyKeys.CongergationName, String.Format("[{0}]", Regex.Escape(String.Join("", Path.GetInvalidFileNameChars))), "")
			Return New FileInfo(String.Format("{0}\{1}.backup", myAutoBackupLocation, fileName))
		End Get
	End Property

	Private Sub UpsertKeyValue(key As String, value As String)
		If db.MetaDatas.Any(Function(x) x.Key = key) Then
			db.MetaDatas.Single(Function(x) x.Key = key).Value = value
		Else
			Dim newKeyValue As New MetaData
			newKeyValue.Key = key
			newKeyValue.Value = value

			db.MetaDatas.AddObject(newKeyValue)
		End If

		db.SaveChanges()
	End Sub

	Private Function GetKeyValue(key As String) As String
		Try

			Dim singleMetaData As MetaData

			singleMetaData = db.MetaDatas.SingleOrDefault(Function(x) x.Key = key)

			If singleMetaData Is Nothing Then
				Return Nothing
			Else
				Return singleMetaData.Value
			End If
		Catch ex As Exception
			Return Nothing
		End Try
	End Function

End Module
